package com.qipay.mapper;

import com.qipay.baas.model.Banks;
import com.qipay.baas.model.CpWithdrawTradlog;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface CpWithdrawTradlogMapper extends com.qipay.baas.mapper.CpWithdrawTradlogMapper {

    @SelectProvider(type = CpWithdrawTradlogSqlProvider.class, method = "count")
    long count(Long cpId,String orderNo,Byte state, String from, String to);

    @Select({"select IFNULL(SUM(amount),0)amount from cp_withdraw_tradlog where cp_id=#{cpId,jdbcType=BIGINT} and state in(0,2)"})
    long countCpAmount(@Param("cpId") Long cpId);

    @SelectProvider(type = CpWithdrawTradlogSqlProvider.class, method = "countApply")
    long countApply();


//    @Select("select * from banks where province like '%#{province,jdbcType=VARCHAR}%' "
//    +"and city like '%#{city,jdbcType=VARCHAR}%' and bank_name like '%#{bank,jdbcType=VARCHAR}%'")
@SelectProvider(type = CpWithdrawTradlogSqlProvider.class, method = "findSubbranch")
    List<Banks> findSubbranch(String province,String city,String bank);

    @Update({
            "update cp_withdraw_tradlog ",
            "set hasRemind = 1",
            "where hasRemind is null;"
    })
    void updateRemind();

    //查询统计行信息
    @SelectProvider(type = CpWithdrawTradlogSqlProvider.class, method = "countInfo")
    @Results({
            @Result(column = "amountCount", property = "amount", jdbcType = JdbcType.BIGINT),
            @Result(column = "feeCount", property = "fee", jdbcType = JdbcType.DOUBLE),
            @Result(column = "actualAmountCount", property = "actualAmount", jdbcType = JdbcType.BIGINT),
    })
    CpWithdrawTradlog countInfo(Long cpId,String orderNo,Byte state, String from, String to);

    @SelectProvider(type = CpWithdrawTradlogSqlProvider.class, method = "list")
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "cp_id", property = "cpId", jdbcType = JdbcType.BIGINT),
            @Result(column = "amount", property = "amount", jdbcType = JdbcType.DOUBLE),
            @Result(column = "fee", property = "fee", jdbcType = JdbcType.DOUBLE),
            @Result(column = "actual_amount", property = "actualAmount", jdbcType = JdbcType.DOUBLE),
            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
            @Result(column = "card_bank", property = "cardBank", jdbcType = JdbcType.VARCHAR),
            @Result(column = "card_sub_bank", property = "cardSubBank", jdbcType = JdbcType.VARCHAR),
            @Result(column = "card_no", property = "cardNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "mobile", property = "mobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "trade_no", property = "tradeNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "out_trade_no", property = "outTradeNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "attach", property = "attach", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "state", property = "state", jdbcType = JdbcType.TINYINT),
            @Result(column = "operation", property = "operation", jdbcType = JdbcType.TINYINT),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "finish_time", property = "finishTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "province", property = "province", jdbcType = JdbcType.VARCHAR),
            @Result(column = "city", property = "city", jdbcType = JdbcType.VARCHAR),
            @Result(column = "area", property = "area", jdbcType = JdbcType.VARCHAR)
    })
    List<CpWithdrawTradlog> list(Long cpId,String orderNo, Byte state,String from, String to, int i, Integer pz);

    @SelectProvider(type = CpWithdrawTradlogSqlProvider.class, method = "listByCpId")
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "cp_id", property = "cpId", jdbcType = JdbcType.BIGINT),
            @Result(column = "amount", property = "amount", jdbcType = JdbcType.DOUBLE),
            @Result(column = "fee", property = "fee", jdbcType = JdbcType.DOUBLE),
            @Result(column = "actual_amount", property = "actualAmount", jdbcType = JdbcType.DOUBLE),
            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
            @Result(column = "card_bank", property = "cardBank", jdbcType = JdbcType.VARCHAR),
            @Result(column = "card_sub_bank", property = "cardSubBank", jdbcType = JdbcType.VARCHAR),
            @Result(column = "card_no", property = "cardNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "mobile", property = "mobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "trade_no", property = "tradeNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "out_trade_no", property = "outTradeNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "attach", property = "attach", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "state", property = "state", jdbcType = JdbcType.TINYINT),
            @Result(column = "operation", property = "operation", jdbcType = JdbcType.TINYINT),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "finish_time", property = "finishTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "province", property = "province", jdbcType = JdbcType.VARCHAR),
            @Result(column = "city", property = "city", jdbcType = JdbcType.VARCHAR),
            @Result(column = "area", property = "area", jdbcType = JdbcType.VARCHAR)
    })
    List<CpWithdrawTradlog> listByCpId(Long cpId, Byte state);

    @Update({
            "update cp_withdraw_tradlog ",
            "set out_trade_no = #{outTradeNo,jdbcType=VARCHAR},",
            "remark = #{remark,jdbcType=VARCHAR},",
            "`state` = 3,",
            "finish_time = now() ",
            "where id = #{id,jdbcType=BIGINT}"
    })
    void fail(@Param("id") Long id, @Param("outTradeNo") String outTradeNo, @Param("remark") String remark);

    @Update({
            "update cp_withdraw_tradlog ",
            "set out_trade_no = #{outTradeNo,jdbcType=VARCHAR},",
            "`state` = 2,",
            "finish_time = now() ",
            "where id = #{id,jdbcType=BIGINT}"
    })
    void success(@Param("id") Long id, @Param("outTradeNo") String outTradeNo);

    @SelectProvider(type = CpWithdrawTradlogSqlProvider.class, method = "listByTime")
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "cp_id", property = "cpId", jdbcType = JdbcType.BIGINT),
            @Result(column = "amount", property = "amount", jdbcType = JdbcType.DOUBLE),
            @Result(column = "fee", property = "fee", jdbcType = JdbcType.DOUBLE),
            @Result(column = "actual_amount", property = "actualAmount", jdbcType = JdbcType.DOUBLE),
            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
            @Result(column = "card_bank", property = "cardBank", jdbcType = JdbcType.VARCHAR),
            @Result(column = "card_sub_bank", property = "cardSubBank", jdbcType = JdbcType.VARCHAR),
            @Result(column = "card_no", property = "cardNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "mobile", property = "mobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "trade_no", property = "tradeNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "out_trade_no", property = "outTradeNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "attach", property = "attach", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "state", property = "state", jdbcType = JdbcType.TINYINT),
            @Result(column = "operation", property = "operation", jdbcType = JdbcType.TINYINT),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "finish_time", property = "finishTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "province", property = "province", jdbcType = JdbcType.VARCHAR),
            @Result(column = "city", property = "city", jdbcType = JdbcType.VARCHAR),
            @Result(column = "area", property = "area", jdbcType = JdbcType.VARCHAR)
    })
    List<CpWithdrawTradlog> listByTime(String from, String to);

    @SelectProvider(type = CpWithdrawTradlogSqlProvider.class, method = "select")
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "cp_id", property = "cpId", jdbcType = JdbcType.BIGINT),
            @Result(column = "amount", property = "amount", jdbcType = JdbcType.DOUBLE),
            @Result(column = "fee", property = "fee", jdbcType = JdbcType.DOUBLE),
            @Result(column = "actual_amount", property = "actualAmount", jdbcType = JdbcType.DOUBLE),
            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
            @Result(column = "card_bank", property = "cardBank", jdbcType = JdbcType.VARCHAR),
            @Result(column = "card_sub_bank", property = "cardSubBank", jdbcType = JdbcType.VARCHAR),
            @Result(column = "card_no", property = "cardNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "mobile", property = "mobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "trade_no", property = "tradeNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "out_trade_no", property = "outTradeNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "attach", property = "attach", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "state", property = "state", jdbcType = JdbcType.TINYINT),
            @Result(column = "operation", property = "operation", jdbcType = JdbcType.TINYINT),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "finish_time", property = "finishTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "province", property = "province", jdbcType = JdbcType.VARCHAR),
            @Result(column = "city", property = "city", jdbcType = JdbcType.VARCHAR),
            @Result(column = "area", property = "area", jdbcType = JdbcType.VARCHAR)
    })
    List<CpWithdrawTradlog> select(CpWithdrawTradlog cpWithdrawTradlog);

//    @SelectProvider(type = CpWithdrawTradlogSqlProvider.class, method = "listBy")
//    @Results({
//            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
//            @Result(column = "cp_id", property = "cpId", jdbcType = JdbcType.BIGINT),
//            @Result(column = "amount", property = "amount", jdbcType = JdbcType.DOUBLE),
//            @Result(column = "fee", property = "fee", jdbcType = JdbcType.DOUBLE),
//            @Result(column = "actual_amount", property = "actualAmount", jdbcType = JdbcType.DOUBLE),
//            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
//            @Result(column = "card_bank", property = "cardBank", jdbcType = JdbcType.VARCHAR),
//            @Result(column = "card_sub_bank", property = "cardSubBank", jdbcType = JdbcType.VARCHAR),
//            @Result(column = "card_no", property = "cardNo", jdbcType = JdbcType.VARCHAR),
//            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
//            @Result(column = "mobile", property = "mobile", jdbcType = JdbcType.VARCHAR),
//            @Result(column = "trade_no", property = "tradeNo", jdbcType = JdbcType.VARCHAR),
//            @Result(column = "out_trade_no", property = "outTradeNo", jdbcType = JdbcType.VARCHAR),
//            @Result(column = "attach", property = "attach", jdbcType = JdbcType.VARCHAR),
//            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
//            @Result(column = "state", property = "state", jdbcType = JdbcType.TINYINT),
//            @Result(column = "operation", property = "operation", jdbcType = JdbcType.TINYINT),
//            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
//            @Result(column = "finish_time", property = "finishTime", jdbcType = JdbcType.TIMESTAMP),
//            @Result(column = "province", property = "province", jdbcType = JdbcType.VARCHAR),
//            @Result(column = "city", property = "city", jdbcType = JdbcType.VARCHAR),
//            @Result(column = "area", property = "area", jdbcType = JdbcType.VARCHAR)
//    })
//    List<CpWithdrawTradlog> listBy(Map<String, Object> params);

    @SelectProvider(type = CpWithdrawTradlogSqlProvider.class, method = "listByState")
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "cp_id", property = "cpId", jdbcType = JdbcType.BIGINT),
            @Result(column = "amount", property = "amount", jdbcType = JdbcType.DOUBLE),
            @Result(column = "fee", property = "fee", jdbcType = JdbcType.DOUBLE),
            @Result(column = "actual_amount", property = "actualAmount", jdbcType = JdbcType.DOUBLE),
            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
            @Result(column = "state", property = "state", jdbcType = JdbcType.TINYINT),
            @Result(column = "operation", property = "operation", jdbcType = JdbcType.TINYINT),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "finish_time", property = "finishTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "province", property = "province", jdbcType = JdbcType.VARCHAR),
            @Result(column = "city", property = "city", jdbcType = JdbcType.VARCHAR),
            @Result(column = "area", property = "area", jdbcType = JdbcType.VARCHAR)
    })
    List<CpWithdrawTradlog> listByState(Byte state);

    @Update({
            "update cp_withdraw_tradlog ",
            "set `state` = 0",
            "where id = #{id,jdbcType=BIGINT}"
    })
    void updateState(@Param("id") Long id);

    @Select({"select * from cp_withdraw_tradlog where out_trade_no = #{outTradeNo,jdbcType=VARCHAR} limit 1"})
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "cp_id", property = "cpId", jdbcType = JdbcType.BIGINT),
            @Result(column = "amount", property = "amount", jdbcType = JdbcType.DOUBLE),
            @Result(column = "fee", property = "fee", jdbcType = JdbcType.DOUBLE),
            @Result(column = "actual_amount", property = "actualAmount", jdbcType = JdbcType.DOUBLE),
            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
            @Result(column = "card_bank", property = "cardBank", jdbcType = JdbcType.VARCHAR),
            @Result(column = "card_sub_bank", property = "cardSubBank", jdbcType = JdbcType.VARCHAR),
            @Result(column = "card_no", property = "cardNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "mobile", property = "mobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "trade_no", property = "tradeNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "out_trade_no", property = "outTradeNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "attach", property = "attach", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "state", property = "state", jdbcType = JdbcType.TINYINT),
            @Result(column = "operation", property = "operation", jdbcType = JdbcType.TINYINT),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "finish_time", property = "finishTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "province", property = "province", jdbcType = JdbcType.VARCHAR),
            @Result(column = "city", property = "city", jdbcType = JdbcType.VARCHAR),
            @Result(column = "area", property = "area", jdbcType = JdbcType.VARCHAR)
    })
    CpWithdrawTradlog findByOutTradeNo(@Param("outTradeNo") String outTradeNo);
}